package com.example.linyiquan.login.activity;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.app.AlertDialog;

import com.example.linyiquan.login.R;
import com.example.linyiquan.login.adapter.GridAdapter;
import com.example.linyiquan.login.bean.AppBean;
import com.example.linyiquan.login.util.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by linyiquan on 17-4-13.
 */

public class GridActivity extends FragmentActivity {

    private GridView gridView;
    private List<AppBean> list = new ArrayList<>();
    private GridAdapter gridAdapter;
    SharedPreferences sp;
    SharedPreferences.Editor spe;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview);

        sp = getPreferences(0);
        spe = sp.edit();

        initApps();
        initGrid();
    }

    private void initApps() {
        List<AppBean> apps = Utils.getApps(this);
        Set<String> appPackages = sp.getStringSet("appPackages",new HashSet<String>());
        Iterator iterator = appPackages.iterator();
        int index = 0;
        while (iterator.hasNext()){
            String packageName = (String) iterator.next();
            if(apps.get(index).getPackageInfo().equals(packageName)){
                list.add(apps.get(index));
            }
            index++;
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
        saveData();
    }

    private void saveData() {
        Set<String> appPackages = new HashSet<>();
        for (int i = 0; i < list.size() - 2; i++) {
            appPackages.add(list.get(i).getPackageInfo());
        }
        spe.putStringSet("appPackages",appPackages);
        spe.commit();
    }

    private void initGrid() {

        gridView= (GridView) findViewById(R.id.gridview);
        gridAdapter = new GridAdapter(this,list);
        gridView.setAdapter(gridAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == gridAdapter.getCount() - 1){//remove
                    String[] items = new String[list.size() - 2];
                    for (int i = 0; i < list.size() - 2; i++) {
                        items[i] = list.get(i).getAppName();
                    }
                    new AlertDialog.Builder(GridActivity.this)
                            .setTitle("Delete your app")
                            .setItems(items, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    list.remove(which);
                                    gridAdapter.notifyDataSetChanged();
                                }
                            }).show();
                }else if(position == gridAdapter.getCount() - 2){//add
                    final List<AppBean> apps = Utils.getApps(GridActivity.this);
                    String[] items = new String[apps.size()];
                    for (int i = 0; i < apps.size(); i++) {
                        items[i] = apps.get(i).getAppName();
                    }
                    new AlertDialog.Builder(GridActivity.this)
                            .setTitle("Add your app")
                            .setItems(items, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    list.add(list.size() - 2 ,apps.get(which));
                                    gridAdapter.notifyDataSetChanged();
                                }
                            }).show();

                }
            }
        });
    }


}
